#!@SHELL@
## this is a bash script
## perhaps it works with other shells too but no warranty

## Written by Michael Bell for the OpenCA project 2004
## (c) Copyright 2004 The OpenCA Project. All rights reserved.
## Modified by Massimiliano Pala <madwolf@openca.org>

LD_LIBRARY_PATH=@openssl_prefix@/lib:$LD_LIBRARY_PATH

export LD_LIBRARY_PATH

sys=`uname`
STAT=1

set -e

prefix=@prefix@
exec_prefix=@exec_prefix@
var_prefix=@var_prefix@

if [ `uname | grep -i "BSD"` ] ; then
   STAT=0;
else
	if [ `uname | grep -i "arwin"` ] ; then
   		STAT=0;
	else
		STAT=1;
	fi
fi

[ $STAT -gt 0 ] && cf_mod=`stat -c %Y @etc_prefix@/config.xml`

for DIRECTORY in @etc_prefix@ @lib_prefix@ @ca_htdocs_fs_prefix@ @ra_htdocs_fs_prefix@ @node_htdocs_fs_prefix@ @ldap_htdocs_fs_prefix@ @pub_htdocs_fs_prefix@ @batch_htdocs_fs_prefix@ @etc_prefix@/servers @etc_prefix@/agreements @etc_prefix@/menus


do
    if test -d $DIRECTORY; then
        echo =================================================================
        echo fixing directory: $DIRECTORY
        echo =================================================================

        cd $DIRECTORY
        TEMPLATES=`find . -name "*.template" -print`
        for H in $TEMPLATES
        do
	    [ $STAT -gt 0 ] && template_mod=`stat -c %Y $H`

            echo -------------begin file-----------------
            echo template: $H
            H_RESULT=`echo $H | sed 's/\.template//'`
            echo target:   $H_RESULT

	    if [ $STAT -gt 0 ] ; then

	    	if ! [ -f "$H_RESULT" ] ; then 
			temp_mod=0
	    	else
	    		temp_mod=`stat -c %Y $H_RESULT`
	    	fi

	    	UPDATE=0

	    	if [ $template_mod -ge $temp_mod ] ; then
			UPDATE=1
	    	else
			if [ $cf_mod -ge $temp_mod ] ; then
				UPDATE=2
			fi
		fi
	    else
		UPDATE=1
	    fi

	    echo "UPDATE => $UPDATE"
            if [ $UPDATE -gt 0 ] ; then
                echo status:   To be updated
            	@bindir@/openca-configure @etc_prefix@/config.xml $H $H_RESULT
            else
                echo status:   Updated
            fi
            echo --------------end file------------------
        done
    fi
done

# By this moment configure_etc.sh has successfully inserted the value of
# option cert_chars from config.xml into utf8_latin1_selector.sh 
# and we can call the latter from here, performing an effective reqursion:

@etc_prefix@/utf8_latin1_selector.sh

# Now we translate the menus

@bindir@/openca-menu-translate

# Now we need to be sure that no issue is present for log writing

@CHOWN@ -R "@httpd_user@:@httpd_group@" "@var_prefix@/log/xml/"

exit 0;

